﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Osteox.ManagedCv;
using Osteox.ManagedCv.WinForm;

namespace WinFormSample
{
    public partial class BrowseImagesDialog : Form
    {
        public BrowseImagesDialog(IEnumerable<ManagedImage> images)
        {
            InitializeComponent();
            foreach (ManagedImage image in images)
            {
                ImageViewer viewer = new ImageViewer();
                viewer.BackColor = Color.LightYellow;
                viewer.Dock = DockStyle.Fill;
                viewer.SizeMode = PictureBoxSizeMode.Zoom;
                viewer.Image = image;

                CheckBox checker = new CheckBox();
                checker.BackColor = Color.LightYellow;
                checker.Text = string.Empty;
                checker.AutoSize = true;
                checker.Tag = image;
                checker.Left = 0;
                checker.Top = 0;
                checker.CheckedChanged += new EventHandler(OnCheckerCheckedChanged);

                Panel panel = new Panel();
                panel.Dock = DockStyle.Fill;
                panel.Controls.Add(checker);
                panel.Controls.Add(viewer);

                tableLayoutPanel.Controls.Add(panel);
            }
        }

        void OnCheckerCheckedChanged(object sender, EventArgs e)
        {
        }

        public ICollection<ManagedImage> Selected
        {
            get
            {
                List<ManagedImage> collection = new List<ManagedImage>();
                foreach (Control control in tableLayoutPanel.Controls)
                {
                    if (control.Controls.Count == 2 && control is Panel)
                    {
                        CheckBox checker = control.Controls[0] as CheckBox;
                        if (checker.Checked)
                        {
                            collection.Add(checker.Tag as ManagedImage);
                        }
                    }
                }
                return collection;
            }
        }

        private void OnClearSelection(object sender, EventArgs e)
        {
            foreach (Control control in tableLayoutPanel.Controls)
            {
                if (control.Controls.Count == 2 && control is Panel)
                {
                    CheckBox checker = control.Controls[0] as CheckBox;
                    checker.Checked = false;
                }
            }
        }

        private void checkBoxAll_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBoxAll.CheckState == CheckState.Indeterminate)
            {
                return;
            }
            foreach (Control control in tableLayoutPanel.Controls)
            {
                if (control.Controls.Count == 2 && control is Panel)
                {
                    CheckBox checker = control.Controls[0] as CheckBox;
                    checker.Checked = checkBoxAll.Checked;
                }
            }
        }
    }
}
